package com.ewanghuiju.app.ui.my.fragment;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseFragment;
import com.ewanghuiju.app.base.contract.my.MyTeamContract;
import com.ewanghuiju.app.model.bean.local.FansInfoBean;
import com.ewanghuiju.app.model.bean.response.TeamInfoResponBean;
import com.ewanghuiju.app.presenter.my.MyTeamPresenter;
import com.ewanghuiju.app.ui.my.adapter.MyTeamListAdapter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class 我的团队列表页面
 * @time 2020/2/28
 */
public class MyTeamFragment extends BaseFragment<MyTeamPresenter> implements MyTeamContract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private int mType;

    private MyTeamListAdapter adapter;

    private int currentPage = 1;

    private int currentType = 1;

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_team;
    }

    @Override
    protected void initEventAndData() {
        initRecyleview();
        initRefreshLayout();
    }


    private void initRefreshLayout() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(activity);
                currentPage++;
                mPresenter.getData(currentPage, "",currentType);
            }
        });
        LoadingDialogUtils.show(activity);
        mPresenter.getData(currentPage,"",currentType);
    }

    private void initRecyleview() {

        recyclerview.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerview.setLayoutManager(linearLayoutManager);
        adapter = new MyTeamListAdapter(R.layout.item_my_team_list);
        adapter.setHasStableIds(true);
        recyclerview.setAdapter(adapter);
    }

    @Override
    public void initImmersionBar() {
//        super.initImmersionBar();
    }

    @Override
    public void showContent(TeamInfoResponBean bean) {
        //获取信息成功
        LoadingDialogUtils.dismissDialog_ios();
        if (bean == null) {
            return;
        }
        List<FansInfoBean> list = bean.getList();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
            adapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE_TWO) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            adapter.addData(list);
        }


    }

    @Override
    public void showContentError() {
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
    }
}
